/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.gwt.client.service.content;

import han.bifang.gwt.client.common.EntityConstants;
import han.bifang.gwt.client.common.GwtContext;
import han.bifang.gwt.client.common.PrivilegChecker;
import han.bifang.gwt.client.common.RequestObject;
import han.bifang.gwt.client.common.ResponseObject;
import han.bifang.gwt.client.common.entity.Service;
import han.bifang.gwt.client.common.util.StringUtils;
import han.bifang.gwt.client.communicate.BifangGWTServiceAsync;
import han.bifang.gwt.client.service.ServiceMessages;

import java.util.ArrayList;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;
import com.google.gwt.user.client.ui.HTMLTable.ColumnFormatter;

/**
 * 
 * @Author Kent
 */
public class ServiceBasicContent extends Composite {

	private BifangGWTServiceAsync service = null;

	private GwtContext context = null;

	private ServiceMessages cons;

	private VerticalPanel panel = null;

	private TextBox name, code;
	private TextArea desc;
	private ListBox ver, status;

	private Service ser;
	private ArrayList vl = null;

	public ServiceBasicContent(ServiceMessages cons,
			BifangGWTServiceAsync service, GwtContext context, Service ser,
			ArrayList vl) {
		this.cons = cons;
		this.service = service;
		this.context = context;
		this.ser = ser;
		this.vl = vl;
		panel = new VerticalPanel();
		panel.setSpacing(5);
		panel.setWidth("100%");

		initWidget(panel);

	}

	private AsyncCallback<ResponseObject> cb = new AsyncCallback<ResponseObject>() {
		public void onSuccess(ResponseObject res) {
			String code1 = res.getCode();
			if (!code1.equals("0")) {
				Window.alert("There was an error: " + res.getMsg());
				return;
			}

			Window.alert(cons.modifyOK());

		}

		public void onFailure(Throwable caught) {
			Window.alert("There was an error: " + caught.toString());
		}
	};

	public void onLoad() {
		HTML h = new HTML(cons.serviceBasicInfo());
		panel.add(h);

		Grid table = new Grid(6, 2);
		table.setCellPadding(1);
		table.setCellSpacing(1);
		table.setBorderWidth(1);

		ColumnFormatter cf = table.getColumnFormatter();
		cf.setWidth(0, "15%");
		cf.setWidth(1, "45%");

		CellFormatter ff = table.getCellFormatter();
		ff.setStyleName(0, 0, "tablecol");
		ff.setStyleName(1, 0, "tablecol");
		ff.setStyleName(2, 0, "tablecol");
		ff.setStyleName(3, 0, "tablecol");
		ff.setStyleName(4, 0, "tablecol");
		ff.setStyleName(5, 0, "tablecol");

		table.setHTML(0, 0, cons.name());
		table.setHTML(1, 0, cons.code());
		table.setHTML(2, 0, cons.defaultVersion());
		table.setHTML(3, 0, cons.createTime());
		table.setHTML(4, 0, cons.status());
		table.setHTML(5, 0, cons.desc());

		name = new TextBox();
		name.setVisibleLength(32);

		code = new TextBox();
		code.setVisibleLength(32);

		desc = new TextArea();
		desc.setCharacterWidth(64);
		desc.setVisibleLines(6);

		name.setText(ser.getServiceName());
		code.setText(ser.getServiceCode());
		desc.setText(ser.getDescription());

		table.setWidget(0, 1, name);
		table.setWidget(1, 1, code);
		ver = new ListBox();
		ver.setWidth("11em");
		ver.addItem("", "");
		ver.setSelectedIndex(0);
		for (int i = 0, j = vl.size(); i < j; i++) {
			ver.addItem((String) vl.get(i), (String) vl.get(i));
			if (vl.get(i).equals(ser.getDefaultVersion())) {
				ver.setSelectedIndex(i + 1);
			}
		}
		table.setWidget(2, 1, ver);

		table.setHTML(3, 1, ser.getCreateTime());
		status = new ListBox();
		status.addItem(cons.published(),
				EntityConstants.SERVICE_STATUS_PUBLISHED);
		status.addItem(cons.unpublished(),
				EntityConstants.SERVICE_STATUS_UNPUBLISHED);
		if (ser.getStatus().equals(EntityConstants.SERVICE_STATUS_PUBLISHED)) {
			status.setSelectedIndex(0);
		} else {
			status.setSelectedIndex(1);
		}
		table.setWidget(4, 1, status);
		table.setWidget(5, 1, desc);

		Button b = new Button(cons.modify());
		b.addClickListener(new ClickListener() {
			public void onClick(Widget send) {
				if (!validate())
					return;
				ser.setDescription(desc.getText());
				ser.setServiceName(name.getText());
				ser.setServiceCode(code.getText());
				if (ver.getSelectedIndex() > 0) {
					ser.setDefaultVersion(ver.getValue(ver.getSelectedIndex()));
				}
				ser.setStatus(status.getValue(status.getSelectedIndex()));

				RequestObject req = new RequestObject();
				req
						.setAction("han.bifang.gwt.server.action.service.ModifyServiceAction");
				req.setRequest(ser);
				service.call(req, cb);

			}
		});

		panel.add(table);
		if (PrivilegChecker.hasPrivilege(context, null))
			panel.add(b);

	}

	private boolean validate() {
		if (!StringUtils.checkLenght(name.getText(), 1, 128)) {
			Window.alert(cons.lengthError(cons.name(), "1", "128"));
			return false;
		}
		if (!StringUtils.checkLenght(desc.getText(), 1, 1024)) {
			Window.alert(cons.lengthError(cons.desc(), "1", "1024"));
			return false;

		}
		if (!StringUtils.checkLenght(code.getText(), 1, 32)) {
			Window.alert(cons.lengthError(cons.code(), "1", "32"));
			return false;

		}

		return true;

	}

}
